﻿using Furion.Schedule;
using Microsoft.Extensions.Logging;

namespace ApiLib.Common.MyJobs;

/// <summary>
/// 任务监控
/// </summary>
public class DefaultJobMonitor : IJobMonitor
{
    private readonly ILogger<DefaultJobMonitor> _logger;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="logger"></param>
    public DefaultJobMonitor(ILogger<DefaultJobMonitor> logger)
    {
        _logger = logger;
    }

    /// <summary>
    /// 执行之前
    /// </summary>
    /// <param name="context"></param>
    /// <param name="stoppingToken"></param>
    /// <returns></returns>
    public Task OnExecutingAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        _logger.LogInformation("执行之前：{}", context);
        return Task.CompletedTask;
    }

    /// <summary>
    /// 执行之后
    /// </summary>
    /// <param name="context"></param>
    /// <param name="stoppingToken"></param>
    /// <returns></returns>
    public Task OnExecutedAsync(JobExecutedContext context, CancellationToken stoppingToken)
    {
        _logger.LogInformation("执行之后：{}", context);

        if (context.Exception != null)
        {
            _logger.LogError(context.Exception, "执行出错啦：{}", context);
        }

        return Task.CompletedTask;
    }
}